package ejb.updater;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import ejb.dao.CityDAO;
import ejb.dao.ForecastDAO;
import ejb.entity.City;
import ejb.entity.DefaultForecast;
import ejb.entity.Forecast;
import ejb.manager.GenerateMapManager;
import ejb.manager.NotificationManager;
import ejb.manager.OldCities;
import ejb.manager.PDFManager;

/**
 * Message-Driven Bean implementation class for: TestowyMDB
 *
 */
@MessageDriven(
                activationConfig = { 
                                @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                                @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/KolejkaKomunikatow")}, 
                mappedName = "queue/KolejkaKomunikatow")
public class UpdaterMessageDrivenBean implements MessageListener {
        @EJB
        private CityDAO cityDAO;
        
        @EJB
        private GenerateMapManager gen;
        
        @EJB
        private ForecastDAO fdao;
        
        @EJB
        private NotificationManager nman;
        
        @EJB
        private OldCities oldcities;
        
        @EJB
        private PDFManager pdfManager;
        
    /**
     * Default constructor. 
     */
    public UpdaterMessageDrivenBean() {
        // TODO Auto-generated constructor stub
    }
        
        /**
        * @see MessageListener#onMessage(Message)
        */
        public void onMessage(Message message) {
                try {
                        if(message.getBooleanProperty("update")) {
                                System.out.println("Generowanie mapy...");
                                try {
                                        if(oldcities.isUpdate()) {
                                                oldcities.setUpdate(true);
                                                oldcities.copy();
                                        }
                                        pdfManager.createForecastPdf();
                                        gen.generateMap();
                                        nman.sendNotification(oldcities.getCities());
                                        oldcities.setUpdate(false);
                                } catch(NullPointerException e) {
                                        System.err.println("Null Cities");
                                }
                        } else {
                                if(message.getBooleanProperty("forecast")) {
                                        boolean newForecast = false;
                                        System.out.println("Aktualizacja prognozy...");
                                        AltCityForecast act = (AltCityForecast) ((ObjectMessage)message).getObject();
                                        System.out.println(act);
                                        if(act==null) return;
                                City ct = cityDAO.getById(act.getId());
                                Forecast fc = fdao.findByCityAndDay(ct, act.getPlusday());
                                if(fc==null) {
                                        newForecast = true;
                                        fc = new DefaultForecast();
                                        fc.setCity(ct);
                                }
                                fc.setConditions(act.getConditions());
                                fc.setTemperature(act.getTemperature());
                                fc.setPlusday(act.getPlusday());
                                if(newForecast) 
                                        fdao.create(fc);
                        } else {
                                AltCity act = (AltCity) ((ObjectMessage)message).getObject();
                                if(act==null) return;
                                City ct = cityDAO.getById(act.getId());
                                ct.setTemperature(act.getTemperature());
                                ct.setPressure(act.getPressure());
                                ct.setConditions(act.getConditions());
                        }
                }
                } catch (JMSException e) {
                        e.printStackTrace();
                }
        
    }

}